<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.mselect.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.multiselect.js"></script>
<style>
.blur {
	cursor: pointer;
	border: none;
}
</style>
<script type="text/javascript" >
get_check=function(){
	var ids = '';
	$("#list_table").find("input[type='checkbox']").each(function(){
		if ($(this).is(":checked"))
		{
			if (ids)
			{
				ids += ',';
			}
			ids += $(this).val();
		}
	});
	return ids;
};
$(function(){
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	var number_old;
	$(".blur").focus(function(){
		$(this).removeClass("blur");
		number_old = $(this).val();
	}).blur(function(){
		var current_tag = $(this);
		var id = $(this).next().val();
		var number_new = $(this).val();
		if (number_old != number_new)
		{
			if ($(this).attr("name") == 'tracking_number')
			{
				var url = "<?=url('Sales::Order/TrackingNumber')?>";
			}
			if ($(this).attr("name") == 'express_number')
			{
				var url = "<?=url('Sales::Order/ExpressNumber')?>";
			}
			$.ajax({
				url: url + "&id=" + id + "&number=" + number_new,
				type: "GET",
				dataType: "json",
				success:
					function(data)
					{
					}
			});
		}
		current_tag.addClass("blur");
	});
	$(".syn_flag").change(function(){
		var id = $(this).next().val();
		var url = "<?=url('Sales::Order/UpdateSynFlag')?>";
		$.ajax({
			url: url + "&id=" + id + "&syn_flag=" + $(this).val(),
			type: "GET",
			dataType: "json",
			success:
				function(data)
				{
				}
		});
	});
	$("#multiselect").multiselect({height: 260, minWidth: 160});
	$("#check_all_1").click(function(){
		if ($(this).is(":checked"))
		{
			$(".check1 input[type='checkbox']:enabled").attr("checked", true);
		}
		else
		{
			$(".check1 input[type='checkbox']:enabled").attr("checked", false);
		}
	});
	$("#check_all_2").click(function(){
		if ($(this).is(":checked"))
		{
			$(".check2 input[type='checkbox']:enabled").attr("checked", true);
		}
		else
		{
			$(".check2 input[type='checkbox']:enabled").attr("checked", false);
		}
	});
	$("#syn_btn").click(function(){
		var ids = get_check();
		var type = $("#mark_shipped_select").val();
		var url = "<?=url('Sales::Order/MarkShipped', array(Q::ini('unique_token_name')=>Helper_BSS_Normal::genToken()))?>&type="+type;
		if ( ! ids)
		{
			alert("请选择订单");
			return false;
		}
		if ( ! type)
		{
			alert("请选择同步类型");
			return false;
		}
		if ( ! confirm('确定同步到'+type+'?'))
		{
			return false;
		}
		$("#syn_ids").val(ids);
		$("#action_form").attr("action", url).submit();
	});
	$("#export_amazon_btn").click(function(){
		var ids = get_check();
		if ( ! ids)
		{
			alert("请选择订单");
			return false;
		}
		$("#syn_ids").val(ids);
		$("#action_form").attr("action", "<?=url('Sales::Order/ExportShipmentFile', array('type' => 'amazon'))?>").submit();
	});
	$("#syn_tracking").click(function(){
		var ids = get_check();
		if ( ! ids)
		{
			alert("请选择订单");
			return false;
		}
		$("#syn_ids").val(ids);
		$("#action_form").attr("action", "<?=url('Sales::Order/PushTracking')?>").submit();
	});
	$("#inquiry_tracking").click(function(){
		var ids = get_check();
		window.open('http://www.17track.net/en/result/post.shtml?nums=' + ids);
	});
});
</script>
<fieldset><legend>查询条件</legend>
<form method="get">
<? include(Q::ini('custom_system/elements_dir') . 'searchform_element.php');?>
<table class="form_table">
<tr>
	<th width="60">确认日期</th>
	<td>
		<input type="text" name="begin_checkout_time" class="datepicker" value="<?=isset($_GET['begin_checkout_time']) ? $_GET['begin_checkout_time'] : ''?>" /> to
		<input type="text" name="end_checkout_time" class="datepicker" value="<?=isset($_GET['end_checkout_time']) ? $_GET['end_checkout_time'] : ''?>" />
	</td>
	<th>发货日期</th>
	<td>
		<input type="text" name="begin_shipped_time" class="datepicker" value="<?=isset($_GET['begin_shipped_time']) ? $_GET['begin_shipped_time'] : ''?>" /> to
		<input type="text" name="end_shipped_time" class="datepicker" value="<?=isset($_GET['end_shipped_time']) ? $_GET['end_shipped_time'] : ''?>" />
	</td>
</tr>
<tr>
	<th>网站店铺</th>
	<td>
		<select name="shop_id[]" multiple="multiple" id="multiselect">
		<?foreach ($shop_group as $k => $sg):?>
		<optgroup label="<?=$k?>">
		<?foreach ($sg as $s):?>
		<option value="<?=$s['id']?>" <?if (isset($_GET['shop_id']) && is_array($_GET['shop_id']) && in_array($s['id'], $_GET['shop_id'])) echo "selected='selected'"?>><?=$s['name']?></option>
		<?endforeach;?>
		</optgroup>
		<?endforeach;?>
		</select>
	</td>
	<th>网站单号</th>
	<td><input type="text" name="shop_order_number" value="<?=isset($_GET['shop_order_number']) ? $_GET['shop_order_number'] : ''?>" /></td>
</tr>
<tr>
	<th>本地单号</th>
	<td><input type="text" name="number" value="<?=isset($_GET['number']) ? $_GET['number'] : ''?>" /></td>
	<th>追踪号</th>
	<td><input type="text" name="tracking_number" value="<?=isset($_GET['tracking_number']) ? $_GET['tracking_number'] : ''?>" /></td>
</tr>
<tr>
	<th>买家ID</th>
	<td><input type="text" name="shop_buyer_id" value="<?=isset($_GET['shop_buyer_id']) ? $_GET['shop_buyer_id'] : ''?>" /></td>
	<th>买家全名</th>
	<td><input type="text" name="fullname" value="<?=isset($_GET['fullname']) ? $_GET['fullname'] : ''?>" /></td>
</tr>
<tr>
	<th>所在国家</th>
	<td><input type="text" name="country_en_cn" value="<?=isset($_GET['country_en_cn']) ? $_GET['country_en_cn'] : ''?>" /></td>
	<th>州/省</th>
	<td><input type="text" name="state" value="<?=isset($_GET['state']) ? $_GET['state'] : ''?>" /></td>
</tr>
<tr>
	<th>物流单号</th>
	<td><input type="text" name="lsp_number" value="<?=isset($_GET['lsp_number']) ? $_GET['lsp_number'] : ''?>" /></td>
</tr>
<tr id="handle_status">
	<th>订单状态</th>
	<td colspan="3">
		<?foreach (Q::ini('custom_flag/sales_order_handle_status') as $os):?>
		<label><input type="checkbox" name="order_status[<?=$os['value']?>]" value="<?=$os['value']?>" <?if (isset($_GET['order_status']) &&  array_key_exists($os['value'], $_GET['order_status'])) echo "checked='checked'"?> /> <?=$os['name']?></label>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<th>邮寄方式</th>
	<td colspan="3">
		<?foreach ($shipping_method as $sm):?>
		<p>
		<?=$sm['name']?>：
		<?foreach ($sm['children'] as $v):?>
		<label><input type="checkbox" name="shipping_method[<?=$v['id']?>]" value="<?=$v['id']?>" <?if (isset($_GET['shipping_method']) && array_key_exists($v['id'], $_GET['shipping_method'])) echo "checked='checked'"?> /> <?=$v['name']?></label>
		<?endforeach;?>
		</p>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<th>同步发货</th>
	<td colspan="3">
		<?foreach (Q::ini('custom_flag/sales_order_mark_shipped_flag') as $m):?>
		<label><input type="checkbox" name="mark_shipped_flag[<?=$m['value']?>]" value="<?=$m['value']?>" <?if (isset($_GET['mark_shipped_flag']) && array_key_exists($m['value'], $_GET['mark_shipped_flag'])) echo "checked='checked'";?> /> <?=$m['name']?></label>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<td></td>
	<td colspan="3">
		<input type="hidden" name="is_search" value="1" />
		<input type="submit" class="button" value="查询" />
	</td>
</tr>
</table>
</form>
</fieldset>
<?if (isset($order)):?>
<table width="100%" class="list_table" id="list_table">
<tr>
	<th>一</th>
	<th>二</th>
	<th width="90">本地单号</th>
	<th>状态</th>
	<th width="60">网站单号</th>
	<th>网站</th>
	<th width="80">销售日期</th>
	<th width="80">发货日期</th>
	<th>买家全名</th>
	<th>国家</th>
	<th width="60">邮寄方式</th>
	<th width="60">同步发货</th>
	<th>追踪号</th>
	<th width="100">物流处理号</th>
	<th>物流标识</th>
</tr>
<?foreach ($order as $o):?>
<tr>
	<td class="check1"><input type="checkbox" value="<?=$o['id']?>" <?if ($o['mark_shipped_flag'] == Q::ini('custom_flag/sales_order_mark_shipped_flag/marked/value')):?> disabled="disabled"<?endif;?> /></td>
	<td class="check2"><input type="checkbox" value="<?=$o['tracking_number']?>" <?if (!$o['tracking_number']):?> disabled="disabled"<?endif;?> /></td>
	<td><?=$o['number']?></td>
	<td><?=Helper_BSS_Normal::getIniOne('sales_order_handle_status', 'value', $o['handle_status'], 'name')?></td>
	<td><?=$o['shop_order_number']?></td>
	<td><?=$o['shop_name']?></td>
	<td><?=substr($o['sales_time'], 0, 10)?></td>
	<td><?=$o['shipped_time'] ? substr($o['shipped_time'], 0, 10) : ''?></td>
	<td><?=$o['fullname']?></td>
	<td><?=isset($o['country_cn']) ? $o['country_cn'] : ''?></td>
	<td><?=$o['shipping_method_name']?></td>
	<td><?=Helper_BSS_Normal::getIniOne('sales_order_mark_shipped_flag', 'value', $o['mark_shipped_flag'], 'name')?></td>
	<td>
		<input type="text" name="tracking_number" value="<?=$o['tracking_number']?>" size="15" class="blur" />
		<input type="hidden" value="<?=$o['id']?>" />
	</td>
	<td>
		<input type="text" name="express_number" value="<?=$o['lsp_number']?>" size="21" class="blur" />
		<input type="hidden" value="<?=$o['id']?>" />
	</td>
	<td>
		<select name="syn_flag" class="syn_flag">
			<option value="">请选择..</option>
			<?php foreach (Q::ini('custom_flag/sales_order_synchronous_flag') as $so):?>
			<option value="<?php echo $so['value']?>" <?php if ($so['value'] == $o['synchronous_flag'])echo "selected='selected'"?>><?php echo $so['name']?></option>
			<?php endforeach;?>
		</select>
		<input type="hidden" value="<?=$o['id']?>" />
	</td>
</tr>
<?endforeach;?>
</table>
<?=$page?>
<table class="list_table">
<tr>
	<td><label><input type="checkbox" id="check_all_1" /> 全选一</label></td>
	<td align="left">
		同步发货状态到：
		<select id="mark_shipped_select">
			<option value="">请选择..</option>
			<option value="Ebay">Ebay</option>
			<option value="Orr">Orr</option>
			<option value="IAINSWAN">IAINSWAN</option>
			<option value="DAVIDSONNE">DAVIDSONNE</option>
			<option value="robertaspland">robertaspland</option>
			<option value="NNJYCE">NNJYCE</option>
			<option value="shariwilliams">shariwilliams</option>
			<option value="TODD">TODD</option>
		</select>
		<input type="button" class="button" id="syn_btn" value="同步" />
	</td>
	<td align="left">
		按格式导出：<input type="button" class="button" id="export_amazon_btn" value="Amazon格式" />
	</td>
	<td align="left">
		推送追踪号到tracking平台 :
		<input type="button" class="button" id="syn_tracking" value="推送" />
	</td>
</tr>
<tr>
	<td><label><input type="checkbox" id="check_all_2" /> 全选二</label></td>
	<td align="left" colspan="3">
		按勾选查询追踪号:
		<input type="button" class="button" id="inquiry_tracking" value="查询" />
	</td>
</tr>
</table>
<form id="action_form" action="" method="post">
<input type="hidden" name="ids" id="syn_ids" />
</form>
<?endif;?>
<?php $this->_endblock();?>